﻿using System.Linq.PageList;
using Dyao.Book.Application.Abstractions;
using Dyao.Book.Contract;
using Dyao.Book.Core;
using Dyao.Book.Web.Extensions;
using Microsoft.AspNetCore.Mvc;

namespace Dyao.Book.Web.Controllers
{
    public class BookSourceSettingController : ApiV1BaseController
    {
        private readonly IBookSourceSettingApplication _settingApp;

        public BookSourceSettingController(IBookSourceSettingApplication settingApp)
        {
            _settingApp = settingApp;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="filterModel">筛选</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<PageList<BookSourceSettingResultDto>>> GetPageListAsync(BookSourceSettingFilterDto filterModel, CancellationToken cancellationToken = default)
        {
            var filter = filterModel;
            var pageFilter = filter.ToPageFilter(filter);
            return (PageList<BookSourceSettingResultDto>)await _settingApp.GetPageListAsync(pageFilter, cancellationToken);
        }

        /// <summary>
        /// 添加配置信息
        /// </summary>
        /// <param name="setting">配置信息</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<string>> AddAsync(BookSourceSettingInfoDto setting, CancellationToken cancellationToken = default)
        {
            this.ValidateModelState();
            var msg = await _settingApp.AddAsync(setting, cancellationToken);
            return msg;
        }

        /// <summary>
        /// 修改配置信息
        /// </summary>
        /// <param name="setting">配置信息</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<string>> ModifyAsync(BookSourceSettingInfoDto setting, CancellationToken cancellationToken = default)
        {
            this.ValidateModelState();
            var msg = await _settingApp.ModifyAsync(setting, cancellationToken);
            return msg;
        }

        /// <summary>
        /// 删除配置信息
        /// </summary>
        /// <param name="id">配置编号</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<string>> DeleteAsync(CommonIdDto id, CancellationToken cancellationToken = default)
        {
            var msg = await _settingApp.DeleteAsync(id.Id, cancellationToken);
            return msg;
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="id">配置编号</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<string>> SwitchEnableAsync(CommonIdDto id, CancellationToken cancellationToken = default)
        {
            var msg = await _settingApp.SwitchEnableAsync(id.Id, cancellationToken);
            return msg;
        }
    }
}
